package cn.walls1717.wallsmall.member.controller;

import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.R;
import cn.walls1717.wallsmall.member.entity.MemberEntity;
import cn.walls1717.wallsmall.member.exception.PhoneExistException;
import cn.walls1717.wallsmall.member.exception.UsernameExistException;
import cn.walls1717.wallsmall.member.service.MemberService;
import cn.walls1717.wallsmall.member.vo.MemberLoginVO;
import cn.walls1717.wallsmall.member.vo.MemberRegisterVO;
import cn.walls1717.wallsmall.member.vo.SocialUserVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import static cn.walls1717.wallsmall.common.exception.BizCodeEnum.*;


/**
 * 会员
 *
 * @author walls1717
 * @email walls1717@163.com
 * @date 2022-10-21 13:14:34
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    /** 第三方登录 */
    @PostMapping("/oauth2/login")
    public R socialLogin(@RequestBody SocialUserVO loginVO) throws ExecutionException, InterruptedException {
        MemberEntity member = memberService.socialLogin(loginVO);
        return R.ok().put("data", member);
    }

    /** 用户登录 */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVO loginVO) {
        MemberEntity member = memberService.getMemberByNameAndPassword(loginVO);
        if (member == null) {
            return R.error(ACCOUNT_PASSWORD_INVALID_EXCEPTION.getCode(), ACCOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
        return R.ok().put("data", member);
    }

    /** 用户注册 */
    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVO registerVO) {
        try {
            memberService.saveMember(registerVO);
        } catch (PhoneExistException e) {
            return R.error(PHONE_EXIST_EXCEPTION.getCode(), PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistException e) {
            return R.error(USER_EXIST_EXCEPTION.getCode(), USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save");
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
